/* 由 ecpg 处理（回归模式） */
/* 这些包含文件由预处理器添加 */
#include <ecpglib.h>
#include <ecpgerrno.h>
#include <sqlca.h>
/* 自动包含部分结束 */
#define ECPGdebug(X,Y) ECPGdebug((X)+100,(Y))

#line 1 "variable.pgc"
#include <stdlib.h>
#include <string.h>


#line 1 "regression.h"






#line 4 "variable.pgc"


/* 执行 sql 每当 sql 错误 停止 ; */
#line 6 "variable.pgc"


/* exec sql type c is char reference */
#line 8 "variable.pgc"

typedef char* c;

/* exec sql 类型 ind 是联合 { 
#line 11 "variable.pgc"
 int integer ;
 
#line 11 "variable.pgc"
 short smallint ;
 } */
#line 11 "variable.pgc"

typedef union { int integer; short smallint; } ind;

#define BUFFERSIZ 8
/* exec sql type str is [ BUFFERSIZ ] */
#line 15 "variable.pgc"


/* 声明游标 cur 用于选择姓名、出生、年龄、婚姻状况、子女从家庭中 */
#line 18 "variable.pgc"


int main (void)
{
	struct birthinfo { 
#line 23 "variable.pgc"
 long born ;
 
#line 23 "variable.pgc"
 short age ;
 } ;
#line 23 "variable.pgc"

/* 执行 SQL 结束声明部分 */
	 		 
					  
				  
	  	 
					  
				    
	 
	           
	    
	    

#line 27 "variable.pgc"
 struct personal_struct { 
#line 25 "variable.pgc"
  struct varchar_1  { int len; char arr[ BUFFERSIZ ]; }  name ;
 
#line 26 "variable.pgc"
 struct birthinfo birth ;
 } fc_personal , * fc_p ;
 
#line 30 "variable.pgc"
 struct personal_indicator { 
#line 28 "variable.pgc"
 int ind_name ;
 
#line 29 "variable.pgc"
 struct birthinfo ind_birth ;
 } fc_ind_personal , * fc_i ;
 
#line 31 "variable.pgc"
 ind fc_ind_children ;
 struct t1 { 
#line 32 "variable.pgc"
  struct varchar_2  { int len; char arr[ BUFFERSIZ ]; }  name ;
 } ; struct t2 { 
#line 32 "variable.pgc"
  struct varchar_3  { int len; char arr[ BUFFERSIZ ]; }  name ;
 } ; 
#line 33 "variable.pgc"
 static  struct varchar_4  { int len; char arr[ 50 ]; }  fc_vc1 ; static  struct varchar_5  { int len; char arr[ 50 ]; }  fc_vc2 ; static  struct varchar_6  { int len; char arr[ 255 ]; }  fc_vc3 ;
 
#line 34 "variable.pgc"
 static int fc_i1 , fc_i2 , fc_i3 ;
/*
 * 默认连接: con2
 * 非默认连接: con1
 *
 */
#line 35 "variable.pgc"


	
#line 37 "variable.pgc"
 char * fc_married = NULL ;

#line 37 "variable.pgc"

	
#line 38 "variable.pgc"
 long fc_ind_married ;

#line 38 "variable.pgc"

	
#line 39 "variable.pgc"
 ind fc_children ;

#line 39 "variable.pgc"

	int fc_loopcount;
	char fc_msg[128];

        ECPGdebug(1, stderr);

	strcpy(fc_msg, "connect");
	{ ECPGconnect(__LINE__, 0, "ecpg1_regression" , NULL, NULL , NULL, 0); 
#line 46 "variable.pgc"

if (sqlca.sqlcode < 0) exit (1);}
#line 46 "variable.pgc"


	strcpy(fc_msg, "set");
	{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "set datestyle to iso", ECPGt_EOIT, ECPGt_EORT);
#line 49 "variable.pgc"

if (sqlca.sqlcode < 0) exit (1);}
#line 49 "variable.pgc"


	strcpy(fc_msg, "create");
	{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "create table family ( name char ( 8 ) , born integer , age smallint , married date , children integer )", ECPGt_EOIT, ECPGt_EORT);
#line 52 "variable.pgc"

if (sqlca.sqlcode < 0) exit (1);}
#line 52 "variable.pgc"


	strcpy(fc_msg, "insert");
	{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "insert into family ( name , married , children ) values ( 'Mum' , '19870714' , 3 )", ECPGt_EOIT, ECPGt_EORT);
#line 55 "variable.pgc"

if (sqlca.sqlcode < 0) exit (1);}
#line 55 "variable.pgc"

	{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "insert into family ( name , born , married , children ) values ( 'Dad' , '19610721' , '19870714' , 3 )", ECPGt_EOIT, ECPGt_EORT);
#line 56 "variable.pgc"

if (sqlca.sqlcode < 0) exit (1);}
#line 56 "variable.pgc"

	{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "insert into family ( name , age ) values ( 'Child 1' , 16 )", ECPGt_EOIT, ECPGt_EORT);
#line 57 "variable.pgc"

if (sqlca.sqlcode < 0) exit (1);}
#line 57 "variable.pgc"

	{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "insert into family ( name , age ) values ( 'Child 2' , 14 )", ECPGt_EOIT, ECPGt_EORT);
#line 58 "variable.pgc"

if (sqlca.sqlcode < 0) exit (1);}
#line 58 "variable.pgc"

	{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "insert into family ( name , age ) values ( 'Child 3' , 9 )", ECPGt_EOIT, ECPGt_EORT);
#line 59 "variable.pgc"

if (sqlca.sqlcode < 0) exit (1);}
#line 59 "variable.pgc"


	strcpy(fc_msg, "commit");
	{ ECPGtrans(__LINE__, NULL, "commit");
#line 62 "variable.pgc"

if (sqlca.sqlcode < 0) exit (1);}
#line 62 "variable.pgc"


	strcpy(fc_msg, "open");
	{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "declare cur cursor for select name , born , age , married , children from family", ECPGt_EOIT, ECPGt_EORT);
#line 65 "variable.pgc"

if (sqlca.sqlcode < 0) exit (1);}
#line 65 "variable.pgc"


	/* 执行 SQL 每当未找到时  继续 ; */
#line 67 "variable.pgc"


	fc_p=&fc_personal;
	fc_i=&fc_ind_personal;
	memset(fc_i, 0, sizeof(fc_ind_personal));
	for (fc_loopcount = 0; fc_loopcount < 100; fc_loopcount++) {
		strcpy(fc_msg, "fetch");
		{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "fetch cur", ECPGt_EOIT, 
	ECPGt_varchar,&(fc_p->name),(long)BUFFERSIZ,(long)-1,sizeof( struct birthinfo ), 
	ECPGt_int,&(fc_i->ind_name),(long)1,(long)-1,sizeof( struct birthinfo ), 
	ECPGt_long,&(fc_p->birth.born),(long)1,(long)1,sizeof( struct birthinfo ), 
	ECPGt_long,&(fc_i->ind_birth.born),(long)1,(long)1,sizeof( struct birthinfo ), 
	ECPGt_short,&(fc_p->birth.age),(long)1,(long)1,sizeof( struct birthinfo ), 
	ECPGt_short,&(fc_i->ind_birth.age),(long)1,(long)1,sizeof( struct birthinfo ), 
	ECPGt_char,&(fc_married),(long)0,(long)1,(1)*sizeof(char), 
	ECPGt_long,&(fc_ind_married),(long)1,(long)1,sizeof(long), 
	ECPGt_int,&(fc_children.integer),(long)1,(long)1,sizeof(int), 
	ECPGt_short,&(fc_ind_children.smallint),(long)1,(long)1,sizeof(short), ECPGt_EORT);
#line 74 "variable.pgc"

if (sqlca.sqlcode == ECPG_NOT_FOUND) break;
#line 74 "variable.pgc"

if (sqlca.sqlcode < 0) exit (1);}
#line 74 "variable.pgc"

		printf("%8.8s", fc_personal.name.arr);
		if (fc_i->ind_birth.born >= 0)
			printf(", born %ld", fc_personal.birth.born);
		if (fc_i->ind_birth.age >= 0)
			printf(", age = %d", fc_personal.birth.age);
		if (fc_ind_married >= 0)
			printf(", married %s", fc_married);
		if (fc_ind_children.smallint >= 0)
			printf(", children = %d", fc_children.integer);
		putchar('\n');

		free(fc_married);
		fc_married = NULL;
	}

	strcpy(fc_msg, "close");
	{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "close cur", ECPGt_EOIT, ECPGt_EORT);
#line 91 "variable.pgc"

if (sqlca.sqlcode < 0) exit (1);}
#line 91 "variable.pgc"


	strcpy(fc_msg, "drop");
	{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "drop table family", ECPGt_EOIT, ECPGt_EORT);
#line 94 "variable.pgc"

if (sqlca.sqlcode < 0) exit (1);}
#line 94 "variable.pgc"


	strcpy(fc_msg, "commit");
	{ ECPGtrans(__LINE__, NULL, "commit");
#line 97 "variable.pgc"

if (sqlca.sqlcode < 0) exit (1);}
#line 97 "variable.pgc"


	strcpy(fc_msg, "disconnect");
	{ ECPGdisconnect(__LINE__, "CURRENT");
#line 100 "variable.pgc"

if (sqlca.sqlcode < 0) exit (1);}
#line 100 "variable.pgc"


	/* 这只是为了消除未使用变量的警告： */
	fc_vc1.len = fc_vc2.len = fc_vc3.len = 0;
	fc_i1 = fc_i2 = fc_i3 = 0;
	printf("%d %d %d %d %d %d\n",
	       fc_vc1.len, fc_vc2.len, fc_vc3.len,
	       fc_i1, fc_i2, fc_i3);

	return 0;
}
